package com.bjpowernode.config.handler;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.manager.RedisManager;
import com.bjpowernode.result.R;
import com.bjpowernode.utils.ResponseUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 登出处理器
 */
@Component
public class AppLogoutSuccessHandler implements LogoutSuccessHandler {

    @Resource
    private RedisManager redisManager;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        response.setCharacterEncoding("UTF-8");
        TUser tUser = (TUser) authentication.getPrincipal();
        // 将redis中的jwt删除
        redisManager.removeValue(Constants.REDIS_LOGIN_JWT_KEY + tUser.getId());
        R r = R.OK(tUser);
        ResponseUtil.writeResult(r, response);
    }
}
